package com.alibaba.cloud.sentinel.feign;

import feign.Target;
import feign.hystrix.FallbackFactory;
import lombok.AllArgsConstructor;
import org.springframework.cglib.proxy.Enhancer;

@AllArgsConstructor
public class DefaultFallbackFactory<T> implements FallbackFactory<T> {

  private final Target<T> target;

  @Override
  public T create(Throwable cause) {

    final Class<T> type     = target.type();
    final String   name     = target.name();
    Enhancer       enhancer = new Enhancer();
    enhancer.setSuperclass(type);
    enhancer.setUseCache(true);
    enhancer.setCallback(new DefaultFallback<>(type, name, cause));
    return (T)enhancer.create();
  }

}
